//
//  NetWorkManager.swift
//  TestView
//
//  Created by lk@wxm on 2024/12/4.
//

import UIKit

// NetworkManager.swift
import Alamofire

@objc class NetworkManager: NSObject {

    // GET 请求
    @objc func getRequest(
        url: String,
        completion: @escaping @Sendable (_ respons:Any?, Error?) -> Void
    ) {
        AF.request(url,method: .get,
                   encoding: JSONEncoding.default).responseData(completionHandler: { res in
            // response.result为枚举类型，所以需要使用switch
            switch res.result {
            case let .success(Data):
                // 将Data类型的数据转为Json字符串
                let jsonString = try? JSONSerialization.jsonObject(with: Data)
                // 打印json字符串
                print("get success\(String(describing: jsonString))")
                completion(jsonString,nil)
            case let .failure(error):
                print("error\(error)")
            }
            // print("响应数据：\(res.result)")
        })
        
    }
    
    // POST 请求
    
    @objc func postRequest(
        url: String,
        parameters: [String: String],
        completion: @escaping @Sendable (Any?, Error?) -> Void
    ) {
        AF.request(url,method: .post,parameters: parameters,
                   encoding: JSONEncoding.default).responseData(completionHandler: { res in
            // response.result为枚举类型，所以需要使用switch
            switch res.result {
            case let .success(Data):
                // 将Data类型的数据转为Json字符串
                let jsonString = try? JSONSerialization.jsonObject(with: Data)
                // 打印json字符串
                print("post success\(String(describing: jsonString))")
                completion(jsonString,nil)
            case let .failure(error):
                print("error\(error)")
            }
            // print("响应数据：\(res.result)")
        })
    }

}
